// Do not remove the include below
#include "Garduino.h"


//The setup function is called once at startup of the sketch
void setup()
{
	// Add your initialization code here
	// start the Ethernet connection and the server:
	if (!Ethernet.begin(mac)) {
		Serial.println("failed to get an IP address using DHCP, trying manually");
		Ethernet.begin(mac,ip);
	}

	server.begin();

	Serial.begin(9600);
	pinMode(SOIL_PIN,INPUT);
	//Serial.println("DHT TEST PROGRAM ");
	//Serial.print("LIBRARY VERSION: ");
	//Serial.println(DHT_LIB_VERSION);
	//Serial.println();
	Serial.println("Fecha;Status;Humedad ambiente;Temperatura;Punto de condensación;Humedad Tierra");


	// calibrate during the first five seconds
    //sensors.calibration();

	// Start by clearing the Write Protect bit
	// Otherwise the clock data cannot be written
	// The whole register is written,
	// but the WP-bit is the only bit in that register.
	ds1302.DS1302_write (DS1302_ENABLE, 0);

	// Disable Trickle Charger.
	//ds1302.DS1302_write (DS1302_TRICKLE, 0x00);

	// Remove the next define,
	// after the right date and time are set.
//#define SET_DATE_TIME_JUST_ONCE
#ifdef SET_DATE_TIME_JUST_ONCE
	// Set a time and date
	// This also clears the CH (Clock Halt) bit,
	// to start the clock.
	ds1302_struct rtc;
	// Fill the structure with zeros to make
	// any unused bits zero
	memset ((char *) &rtc, 0, sizeof(rtc));

	rtc.Seconds = 0;
	rtc.Seconds10 = 0;
	rtc.CH = 0;        // 1 for Clock Halt, 0 to run;
	rtc.Minutes = 5;
	rtc.Minutes10 = 0;
	rtc.h24.Hour = 3;
	rtc.h24.Hour10 = 2;
	rtc.h24.hour_12_24 = 0; // 0 for 24 hour format
	rtc.Date = 3;
	rtc.Date10 = 0;
	rtc.Month = 3;
	rtc.Month10 = 0;
	rtc.Day = 1;
	rtc.Year = 3;
	rtc.Year10 = 1;
	rtc.WP = 0;

	// Write all clock data at once (burst mode).
	ds1302.DS1302_clock_burst_write( (uint8_t *) &rtc);
#endif
}

// The loop function is called in an endless loop
void loop()
{
	//Add your repeated code here
	// listen for incoming clients
	EthernetClient client = server.available();
//sensors.calibration();
	ds1302_struct rtc;
	char buffer[80];     // the code uses 70 characters.

	if (client) {
		// an http request ends with a blank line
		boolean currentLineIsBlank = true;
		while (client.connected()) {
			if (client.available()) {
				char c = client.read();
				// if you've gotten to the end of the line (received a newline
				// character) and the line is blank, the http request has ended,
				// so you can send a reply
				if (c == '\n' && currentLineIsBlank) {
					// send a standard http response header
					client.println("HTTP/1.1 200 OK");
					client.println("Content-Type: text/html");
					client.println();

					// Read all clock data at once (burst mode).
					ds1302.DS1302_clock_burst_read( (uint8_t *) &rtc);

					sprintf(buffer, "%02d:%02d:%02d %02d/%02d/%d, \t", \
							(rtc.h24.Hour10 * 10) + rtc.h24.Hour, \
							(rtc.Minutes10 * 10) + rtc.Minutes, \
							(rtc.Seconds10 * 10) + rtc.Seconds, \
							(rtc.Date10 * 10) + rtc.Date, \
							(rtc.Month10 * 10) + rtc.Month, \
							2000 + (rtc.Year10 * 10) + rtc.Year);

					// READ DATA
					int chk=sensors.read();
					client.print("DHT11, \t");
					client.print(buffer);
					client.print(Ethernet.localIP());
					client.print(", \t");
					//Serial.print("DHT11, \t");
					//Serial.print(buffer);
					//Serial.print(Ethernet.localIP());
					//Serial.print(", \t");

					switch (chk)
					{
					case DHTLIB_OK:
						client.print("OK,\t");
						//Serial.print("OK,\t");
						break;
					case DHTLIB_ERROR_CHECKSUM:
						client.print("Checksum error,\t");
						//Serial.print("Checksum error,\t");
						break;
					case DHTLIB_ERROR_TIMEOUT:
						client.print("Time out error,\t");
						//Serial.print("Time out error,\t");
						break;
					default:
						client.print("Unknown error,\t");
						//Serial.print("Unknown error,\t");
						break;
					}

					// DISPLAT DATA
					client.print((float)sensors.humidity,3);
					//Serial.print((float)sensors.humidity,3);
					client.print("% ,\t");
					//Serial.print("% ,\t");
					client.print((float)sensors.temp,3);
					//Serial.print((float)sensors.temp,3);
					client.print("oC ,\t");
					//Serial.print("oC ,\t");
					client.print((float)sensors.dewPointD,3);
					//Serial.print((float)sensors.dewPointD,3);
					client.print("oC ,\t");
					//Serial.print("oC ,\t");
					client.print((float)sensors.soilHumidity,3);
					//Serial.print((float)sensors.soilHumidity,3);
					client.println("%");
					//Serial.println("%");

					client.println("<br />");

					break;
				}
				if (c == '\n') {
					// you're starting a new line
					currentLineIsBlank = true;
				}
				else if (c != '\r') {
					// you've gotten a character on the current line
					currentLineIsBlank = false;
				}
			}
		}
		// give the web browser time to receive the data
		delay(1);
		// close the connection:
		client.stop();
	}
	else {
		// READ DATA
		// Read all clock data at once (burst mode).
		ds1302.DS1302_clock_burst_read( (uint8_t *) &rtc);

		sprintf(buffer, "%d/%02d/%02d %02d:%02d:%02d.00;", \
				2000 + (rtc.Year10 * 10) + rtc.Year, \
				(rtc.Month10 * 10) + rtc.Month, \
				(rtc.Date10 * 10) + rtc.Date, \
				(rtc.h24.Hour10 * 10) + rtc.h24.Hour, \
				(rtc.Minutes10 * 10) + rtc.Minutes, \
				(rtc.Seconds10 * 10) + rtc.Seconds);

		//Serial.print("DHT11;");
			Serial.print(buffer);
			//Serial.print(Ethernet.localIP());
		//Serial.print(";");
		int chk=sensors.read();

		switch (chk)
		{
		case DHTLIB_OK:
			Serial.print("OK;");
			break;
		case DHTLIB_ERROR_CHECKSUM:
			Serial.print("Checksum error;");
			break;
		case DHTLIB_ERROR_TIMEOUT:
			Serial.print("Time out error;");
			break;
		default:
			Serial.print("Unknown error;");
			break;
		}
		// DISPLAT DATA
		Serial.print((float)sensors.humidity,3);
		Serial.print(";");
		Serial.print((float)sensors.temp,3);
		Serial.print(";");
		Serial.print((float)sensors.dewPointD,3);
		Serial.print(";");
		Serial.print((float)sensors.soilHumidity,3);
		Serial.println(";");
		delay(1000);

	}
}
